function edgedetect0(num)

iI=num;
t=3;
file='test';
file1=strcat(file,num2str(iI));
p1=strcat(file1,'.tif');

file2=strcat(file,num2str(iI+1));
p2=strcat(file2,'.tif');

%�任��Сsize350*366
t_edge=cputime;
I0=imread(p1);
[Hh,Ww]=size(I0);
% figure,imshow(I0);
%�Աȶ���ǿ
I=ctrenhance(p1);
%I=I0;
%I=double(I);
x21=mat2gray(I);%ת��Ϊ�Ҷ�ͼ�� 0-1
[K,K21]=size(x21);

%%%%%%%%%%%%%%%%��ֵ�˲��õ�y1%%%%%%%%%%%%%%%%%%
m=3;
I11=medfilt2(x21,[m,m]); 
% figure,imshow(I11)
%��I��������λ��para1����һЩ��������morphspotgrid1��ע��
%para2Ϊ����ˮƽ������λ�õľ���
%para3Ϊ���ش�ֱ������λ�õľ���
%hnum=para1(5);%ˮƽ������
%vnum=para1(6);%��ֱ������
%[para1,para2,para3]=morphspotgrid1(I);

[para1,para2,para3,para4,para5]=morphspotgrid(I11);
para2=round(para2);
if para2(1)==0
    para2(1)=1;
end
para3=round(para3);
if para3(1)==0
    para3(1)=1;
end

hnum=size(para2,2);%ˮƽ������
vnum=size(para3,2);%��ֱ������

I=uint8(I11.*256);
[h,w]=size(I);
h1=size(para4,2);
v1=size(para5,2);


% hnum=hnum-2;
% h1=h1-4;


% hnum=hnum+1;
% h1=h1+2;
% para4(1:h1-2)=para41(1:h1-2);
% para4(h1-1)=para4(h1-2)+3;
% para4(h1)=para4(h1-1)+20;
% para2(hnum-1)=para2(hnum-2)+20;
% para2(hnum)=398;
% hnum=hnum+1;
% h1=h1+2;
% para2=[1 22 36 49 62 74 88 101 114 128 141 154 168 180 209];
% para4=[1 11 20 24 33 38 46 51 60 64 72 76 86 90 98 103 112 116 126 130 139 143 152 156 166 170 178 182 192];

t_edge1=cputime;

% t=3;

for j=1:h
    if j<para4(2)-t
        I(j,:)=0;
    end
    for i=3:2:h1-1
       if j>para4(i)+t&&j<para4(i+1)-t
         I(j,:)=0;
       end
    end
    if j>para4(h1)+t
        I(j,:)=0;
    end
end


for j=1:w
    if j<para5(2)-t
        I(:,j)=0;
    end
    for i=1:2:v1-1
      if j>para5(i)+t&&j<para5(i+1)-t
        I(:,j)=0;
      end
    end
    if j>para5(v1)+t
        I(:,j)=0;
    end
end

III=zeros(h,w);
III2=zeros(h,w);
for i1=1:hnum-1
    for j1=1:vnum-1
%         for i2=1:para4(2*i1+1)-para4(2*i1)+2*t-1
%             for j2=1:para5(2*j1+1)-para5(2*j1)+2*t-1
%                 I1(i2,j2)=I(i2+para4(2*i1)-t,j2+para5(2*j1)-t);
        for i2=1:para2(i1+1)-para2(i1)+1  
            for j2=1:para3(j1+1)-para3(j1)+1
                I1(i2,j2)=I(i2+para2(i1)-1,j2+para3(j1)-1);
            end
        end
        [H,W]=size(I1);
BW2=edgedetection(I1);       
Bw_edge=imfill(BW2);
for i=1:H
    for j=1:W
        if BW2(i,j)~=0
            III2(i+para2(i1)-1,j+para3(j1)-1)=1;
        end
        if Bw_edge(i,j)~=0
            III(i+para2(i1)-1,j+para3(j1)-1)=1;
        end
    end
end
clear I1
    end
end

figure,imshow(I0)
for i=1:h
    for j=1:w
        if III2(i,j)==1
            hold on
        plot(j,i,'r');
        end
    end
end
% figure,imshow(III)
% title('edge detection result');
time_edge=cputime-t_edge
time_edge2=cputime-t_edge1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%�������յõ��ķָ�ͼ������������ݵĸ�������
%������������ڵĸ�������
PP1=imread(p1);
PP1=double(PP1);
PP1=PP1./256;        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%��ǰ���������صĻҶ�ֵ��CH1_RAW�ͱ����������صĻҶ�ֵ��CH1_BKD_RAW
%��ǰ���������ҶȾ�ֵ����CH1_MEAN-CH1_BKD_MEAN=CH1_MINUS,ǰ���ͱ����ĻҶȾ�ֵ��
%ͬʱ�����������ڸ����������ǰ�����ظ���CH1_AREA�ͱ������ظ���CH1_BKD_AREA

%CH1_MEAN_MINUS=zeros(hnum-1,vnum-1);%ǰ���ͱ����ĻҶȾ�ֵ��
CH1_RAW=zeros(hnum-1,vnum-1);%ǰ���������صĻҶ�ֵ��
CH1_BKD_RAW=zeros(hnum-1,vnum-1);%�����������صĻҶ�ֵ��
CH1_AREA=zeros(hnum-1,vnum-1);%��¼(i,j)������ ǰ�����ص����
CH1_BKD_AREA=zeros(hnum-1,vnum-1);%��¼(i,j)������ �������ص����
CH1_MEAN=zeros(hnum-1,vnum-1);%ǰ���ĻҶȾ�ֵ
% CH2_MEAN=zeros(hnum-1,vnum-1);
CH1_BKD_MEAN=zeros(hnum-1,vnum-1);%�����ĻҶȾ�ֵ

for i=1:hnum-1
    for j=1:vnum-1
       for ii=para2(i):para2(i+1)
         for jj=para3(j):para3(j+1)
%         for ii=para4(2*i)-t+1:para4(2*i+1)+t-1
%            for jj=para5(2*j)-t+1:para5(2*j+1)+t-1
              %if ii<=h&&jj<=w
            if (III(ii,jj)==1)
               CH1_RAW(i,j)=CH1_RAW(i,j)+PP1(ii,jj);%ǰ���ĻҶ�ֵ��
               CH1_AREA(i,j)=CH1_AREA(i,j)+1;%ǰ�������ظ���
            else
            
            CH1_BKD_RAW(i,j)=CH1_BKD_RAW(i,j)+PP1(ii,jj);%�����ĻҶ�ֵ��
            CH1_BKD_AREA(i,j)=CH1_BKD_AREA(i,j)+1;%���������ظ���
            end
              %end
          end
        end

if CH1_BKD_AREA(i,j)==0    
   CH1_BKD_MEAN(i,j)=0;%�󱳾��ĻҶȾ�ֵ
else
   CH1_BKD_MEAN(i,j)=CH1_BKD_RAW(i,j)/CH1_BKD_AREA(i,j);%�󱳾��ĻҶȾ�ֵ  
end
% CH1_BKD_MEAN(i,j)=CH1_BKD_RAW(i,j)/CH1_BKD_AREA(i,j);%�󱳾��ĻҶȾ�ֵ
if CH1_AREA(i,j)==0    
   CH1_RAW(i,j)=1; 
   CH1_MEAN(i,j)=0;%��ǰ���ĻҶȾ�ֵ
else
   CH1_MEAN(i,j)=CH1_RAW(i,j)/CH1_AREA(i,j);%��ǰ���ĻҶȾ�ֵ
  
end
CH1_MEAN(i,j)=CH1_MEAN(i,j)-CH1_BKD_MEAN(i,j);
    end
end
% xlswrite('k0_CH1_AREA.xls',CH1_AREA)
% xlswrite('k0_CH1_BKD_AREA.xls',CH1_BKD_AREA)
% xlswrite('k0_CH1_MEAN.xls',CH1_MEAN)
% xlswrite('k0_CH2_MEAN.xls',CH2_MEAN)
% xlswrite('k0_CH1_BKD_MEAN.xls',CH1_BKD_MEAN)
% xlswrite('k0_CH1_RAW.xls',CH1_RAW)
% xlswrite('k0_CH1_BKD_RAW.xls',CH1_BKD_RAW)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%��ɹ���
flag=0;
medi1=median(median(CH1_AREA));
for i=1:hnum-1
    for j=1:vnum-1
        if CH1_AREA(i,j)>medi1*0.4&&CH1_AREA(i,j)<medi1*1.5
            flag=flag+1;
        end
    end
end
success1=flag/(i*j)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
t_edge=cputime;
I02=imread(p2);
[Hh,Ww]=size(I02);
%�Աȶ���ǿ
I2=ctrenhance(p2);
%I2=double(I2);
x22=mat2gray(I2);%ת��Ϊ�Ҷ�ͼ�� 0-1
[K12,K22]=size(x22);

%%%%%%%%%%%%%%%%��ֵ�˲��õ�y1%%%%%%%%%%%%%%%%%%
m=3;
I21=medfilt2(x22,[m,m]); 
%��I2��������λ��para21����һЩ��������morphspotgrid1��ע��
%para22Ϊ����ˮƽ������λ�õľ���
%para23Ϊ���ش�ֱ������λ�õľ���
%hnum2=para21(5);%ˮƽ������
%vnum2=para21(6);%��ֱ������
%[para21,para22,para23]=morphspotgrid1(I);

[para1,para2,para3,para4,para5]=morphspotgrid(I21);
para2=round(para2);
if para2(1)==0
    para2(1)=1;
end
para3=round(para3);
if para3(1)==0
    para3(1)=1;
end

hnum=size(para2,2);%ˮƽ������
vnum=size(para3,2);%��ֱ������
%para4


I2=uint8(I21.*256);
[h,w]=size(I2);
h2=size(para4,2);
v2=size(para5,2);

% hnum=hnum-3;
% h2=h2-5;
% para4=para4(1:h2);
% hnum=hnum-1;
% h2=h2-2;
% para4(1:h2)=para41(1:h2);
% para4(h2)=para4(h2)+10;
t_edge1=cputime;
% t=3;


for j=1:h
    if j<para4(2)-t
        I2(j,:)=0;
    end
    for i=3:2:h2-1
       if j>para4(i)+t&&j<para4(i+1)-t
         I2(j,:)=0;
       end
    end
    if j>para4(h2)+t
        I2(j,:)=0;
    end
end


for j=1:w
    if j<para5(2)-t
        I2(:,j)=0;
    end
    for i=1:2:v2-1
      if j>para5(i)+t&&j<para5(i+1)-t
        I2(:,j)=0;
      end
    end
    if j>para5(v2)+t
        I2(:,j)=0;
    end
end

III2=zeros(h,w);
III12=zeros(h,w);
for i1=1:hnum-1
    for j1=1:vnum-1
%         for i2=1:para4(2*i1+1)-para4(2*i1)+2*t-1
%             for j2=1:para5(2*j1+1)-para5(2*j1)+2*t-1
%                 I12(i2,j2)=I2(i2+para4(2*i1)-t,j2+para5(2*j1)-t);
        for i2=1:para2(i1+1)-para2(i1)+1  
            for j2=1:para3(j1+1)-para3(j1)+1
                I12(i2,j2)=I2(i2+para2(i1)-1,j2+para3(j1)-1);                              
            end
        end
        [H,W]=size(I12);
BW22=edgedetection(I12);
Bw_edge2=imfill(BW22);
for i=1:H
    for j=1:W
        if BW22(i,j)~=0
            III12(i+para2(i1)-1,j+para3(j1)-1)=1;
%             III2(i+para4(2*i1)-t,j+para5(2*j1)-t)=1;
        end
        if Bw_edge2(i,j)~=0
            III2(i+para2(i1)-1,j+para3(j1)-1)=1;
%             III2(i+para4(2*i1)-t,j+para5(2*j1)-t)=1;
        end
    end
end

clear I12
    end
end

figure,imshow(I02)
for i=1:h
    for j=1:w
        if III12(i,j)==1
            hold on
        plot(j,i,'r');
        end
    end
end
%title('edge detection result');
time_edge=cputime-t_edge
time_edge2=cputime-t_edge1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%�������յõ��ķָ�ͼ������������ݵĸ�������
%������������ڵĸ�������
        
PP2=imread(p2);
PP2=double(PP2);
PP2=PP2./256;  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%��ǰ���������صĻҶ�ֵ��CH2_RAW�ͱ����������صĻҶ�ֵ��CH2_BKD_RAW
%��ǰ���������ҶȾ�ֵ����CH2_MEAN-CH2_BKD_MEAN=CH2_MINUS,ǰ���ͱ����ĻҶȾ�ֵ��
%ͬʱ�����������ڸ����������ǰ�����ظ���CH2_AREA�ͱ������ظ���CH2_BKD_AREA

%CH2_MEAN_MINUS=zeros(hnum-1,vnum-1);%ǰ���ͱ����ĻҶȾ�ֵ��
CH2_RAW=zeros(hnum-1,vnum-1);%ǰ���������صĻҶ�ֵ��
CH2_BKD_RAW=zeros(hnum-1,vnum-1);%�����������صĻҶ�ֵ��
CH2_AREA=zeros(hnum-1,vnum-1);%��¼(i,j)������ ǰ�����ص����
CH2_BKD_AREA=zeros(hnum-1,vnum-1);%��¼(i,j)������ �������ص����
CH2_MEAN=zeros(hnum-1,vnum-1);%ǰ���ĻҶȾ�ֵ
CH2_BKD_MEAN=zeros(hnum-1,vnum-1);%�����ĻҶȾ�ֵ

for i=1:hnum-1
    for j=1:vnum-1
for ii=para2(i):para2(i+1)
  for jj=para3(j):para3(j+1)
%        for ii=para4(2*i)-t+1:para4(2*i+1)+t-1
%           for jj=para5(2*j)-t+1:para5(2*j+1)+t-1
%if ii<=h&&jj<=w
            if (III2(ii,jj)==1)
               CH2_RAW(i,j)=CH2_RAW(i,j)+PP2(ii,jj);%ǰ���ĻҶ�ֵ��
               CH2_AREA(i,j)=CH2_AREA(i,j)+1;%ǰ�������ظ���
            else
            
            CH2_BKD_RAW(i,j)=CH2_BKD_RAW(i,j)+PP2(ii,jj);%�����ĻҶ�ֵ��
            CH2_BKD_AREA(i,j)=CH2_BKD_AREA(i,j)+1;%���������ظ���
            end
%end
          end
        end

if CH2_BKD_AREA(i,j)==0    
   CH2_BKD_MEAN(i,j)=0;%�󱳾��ĻҶȾ�ֵ
else
   CH2_BKD_MEAN(i,j)=CH2_BKD_RAW(i,j)/CH2_BKD_AREA(i,j);%�󱳾��ĻҶȾ�ֵ  
end
% CH2_BKD_MEAN(i,j)=CH1_BKD_RAW(i,j)/CH2_BKD_AREA(i,j);%�󱳾��ĻҶȾ�ֵ
if CH2_AREA(i,j)==0  
   CH2_RAW(i,j)=1; 
   CH2_MEAN(i,j)=0;%��ǰ���ĻҶȾ�ֵ
        
else
   CH2_MEAN(i,j)=CH2_RAW(i,j)/CH2_AREA(i,j);%��ǰ���ĻҶȾ�ֵ
  
end
CH2_MEAN(i,j)=CH2_MEAN(i,j)-CH2_BKD_MEAN(i,j);
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%��ɹ���
flag=0;
medi2=median(median(CH2_AREA));
for i=1:hnum-1
    for j=1:vnum-1
        if CH2_AREA(i,j)>medi2*0.4&&CH2_AREA(i,j)<medi2*1.5
            flag=flag+1;
        end
    end
end
success2=flag/(i*j)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%ʹ�þ���Ĵ�С��ͬ
% [H1,W1]=size(CH1_RAW);
% [H2,W2]=size(CH2_RAW);
% if H1>=H2
%     CH1_RAW=CH1_RAW(1:H2,1:W1);
% else  CH2_RAW=CH2_RAW(1:H1,1:W2);
% end
% [H1,W1]=size(CH1_RAW);
% [H2,W2]=size(CH2_RAW);
% if W1>=W2
%     CH1_RAW=CH1_RAW(1:H1,1:W2);
% else  CH2_RAW=CH2_RAW(1:H2,1:W1);
% end

%��log2ͼ��������ֵͼ
kmeans_VALUE=zeros((hnum-1)*(vnum-1));
for i=1:hnum-1
    for j=1:vnum-1
        if CH1_AREA(i,j)==0||CH2_AREA(i,j)==0||abs(log2(CH2_MEAN(i,j)/CH1_MEAN(i,j)))>10
           kmeans_VALUE(i,j)=10;
        else
           kmeans_VALUE(i,j)=log2(CH2_MEAN(i,j)/CH1_MEAN(i,j));
        end
    end
end
[HH,WW]=size(kmeans_VALUE);
%�������ֵ��0�ķ����ͣ�������SVM_VAR����
%�������ֵ��0��ƫ���ͣ�������SVM_DEV����
sum1=0;
sum2=0;
for i=1:HH
    for j=1:WW
        X=[kmeans_VALUE(i,j),0];
        sum1=sum1+(std(X))^2;
        sum2=sum2+abs(kmeans_VALUE(i,j));
    end
end

kmeans_VAR=sum1
kmeans_DEV=sum2

figure;
ii=1;
% hold on 
% axis([0 270 -8 10])
for i=1:(hnum-1)
    for j=1:(vnum-1)
       hold on    
        plot(ii,kmeans_VALUE(i,j),'b*');
%         title('ֱ��ͼ��ֵ�����ñ���ֵ');
        ii=ii+1;
    end
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%��������ȡ��ı������ݶ�תΪexcel��ʽ������Ա�
xlswrite('e_CH1_AREA.xls',CH1_AREA)
xlswrite('e_CH1_BKD_AREA.xls',CH1_BKD_AREA)
xlswrite('e_CH1_MEAN.xls',CH1_MEAN)
xlswrite('e_CH1_BKD_MEAN.xls',CH1_BKD_MEAN)
xlswrite('e_CH1_RAW.xls',CH1_RAW)
xlswrite('e_CH1_BKD_RAW.xls',CH1_BKD_RAW)

xlswrite('e_CH2_AREA.xls',CH2_AREA)
xlswrite('e_CH2_BKD_AREA.xls',CH2_BKD_AREA)
xlswrite('e_CH2_MEAN.xls',CH2_MEAN)
xlswrite('e_CH2_BKD_MEAN.xls',CH2_BKD_MEAN)
xlswrite('e_CH2_RAW.xls',CH2_RAW)
xlswrite('e_CH2_BKD_RAW.xls',CH2_BKD_RAW)


xlswrite('e_VALUE.xls',kmeans_VALUE)
% xlswrite('k_VAR.xls',kmeans_VAR)
% xlswrite('k_DEV.xls',kmeans_DEV)
%t_edge=cputime-t_edge
% success1
% success2
end
